package com.runa.persistence.repository;

import com.runa.monitor.comm.entity.CommMeter;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public interface CommMeterRepository extends JpaRepository<CommMeter, String> {

    @Query("SELECT c FROM CommMeter c WHERE c.id IN (:ids)")
    List<CommMeter> getAllByIds(@Param("ids") List<String> ids);

    List<CommMeter> findByNetEquipmentId(String netEquipmentId);

    CommMeter findByNetEquipmentIdAndSerial(String netEquipmentId, String serial);

    List<CommMeter> findBySerial(String serial);

    @Query(value = "select m from CommNetEquipment ne left join CommMeter m on ne.id = m.netEquipmentId " +
            "where ne.serial = :netSerial and m.sequence = :sequence")
    CommMeter getByNetSerialAndSequence(String netSerial, Integer sequence);

    @Query(value = "select m from CommNetEquipment ne left join CommMeter m on ne.id = m.netEquipmentId " +
            "where ne.serial = :netSerial and m.serial = :serial")
    CommMeter getByNetSerialAndSerial(String netSerial, String serial);

    @Query(value = "select m from CommNetEquipment ne left join CommMeter m on ne.id = m.netEquipmentId " +
            "where ne.serial = :netSerial and m.serial like concat('%', :serial) ")
    CommMeter getByNetSerialAndSerialLeftLike(String netSerial, String serial);

}
